/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;
 
import dto.ToaTau;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import util.ConnectToMySql;

public class ToaTauDAO {
       public static List<ToaTau> LayDanhSachToaTau() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
         List<ToaTau> list = new ArrayList<ToaTau>();
        Connection conn = ConnectToMySql.getConnection();
        if (conn != null) {
            try {
                CallableStatement cs = conn.prepareCall("select * from ToaTau ");
                  ResultSet rs = cs.executeQuery();
                if (rs != null) {
                     while (rs.next()) {
                     ToaTau my = new ToaTau();
                    my.setMatoatau(rs.getInt("matoatau"));
                    my.setStttoa(rs.getInt("stttoa"));
                    my.setMatau(rs.getInt("matau"));
                    my.setSoluongghe(rs.getInt("soluongghe"));
                    my.setMaloaitoatau(rs.getInt("maloaitoatau")); 
                    my.setTrangthai(rs.getInt("trangthai"));   
                     list.add(my);
                }
                }
            } catch (Exception ex) {
                System.out.println(ex);
                return null;
            }
        }
        
        return list;
    }
     public static ToaTau LayThongTinToaTau(String matau) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException{
        ToaTau nd = new ToaTau();

        Connection conn = ConnectToMySql.getConnection();
        if (conn != null) {
            try {
                CallableStatement cs = conn.prepareCall("select * from ToaTau where matoatau=?");
                cs.setInt(1, Integer.parseInt(matau)); 
                ResultSet rs = cs.executeQuery();
                if (rs != null) {
                    if (rs.next() == true) {
                        ToaTau my = new ToaTau(); 
                    my.setMatoatau(rs.getInt("matoatau"));
                    my.setStttoa(rs.getInt("stttoa"));
                    my.setMatau(rs.getInt("matau"));
                    my.setSoluongghe(rs.getInt("soluongghe"));
                    my.setMaloaitoatau(rs.getInt("maloaitoatau")); 
                    my.setTrangthai(rs.getInt("trangthai"));   
                     nd= my;
                    } else {
                        nd = null;
                    }
                }
            } catch (Exception ex) {
                return null;
            }
        }
        return nd;
    }
     public static int SoLuongToaTau() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException{
        int kq = 0;
       List<ToaTau> list = null;
        Connection conn = ConnectToMySql.getConnection();
        if (conn != null) {
            try {
                CallableStatement cs = conn.prepareCall("select * from ToaTau");
                  ResultSet rs = cs.executeQuery();
                if (rs != null) {
                     while (rs.next()) {
                     ToaTau my = new ToaTau(); 
                      my.setMatoatau(rs.getInt("matoatau"));
                    my.setStttoa(rs.getInt("stttoa"));
                    my.setMatau(rs.getInt("matau"));
                    my.setSoluongghe(rs.getInt("soluongghe"));
                    my.setMaloaitoatau(rs.getInt("maloaitoatau")); 
                    my.setTrangthai(rs.getInt("trangthai"));   
                     list.add(my);
                }
                }
            } catch (Exception ex) {
                System.out.println(ex); 
            }
        }
        kq= list.size();
        return kq;
    }
     
    public static boolean ThemToaTau(ToaTau nd) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException{
       boolean result = false;

        Connection conn = ConnectToMySql.getConnection();
        if (conn != null) {
            CallableStatement cs = conn.prepareCall("insert into ToaTau(stttoa,matau,soluongghe,trangthai,maloaitoatau)  values(?,?,?,?,?)");
           
           // cs.setString(1, nd.get());
            cs.setInt(1, nd.getStttoa());
            cs.setInt(2, nd.getMatau());
            cs.setInt(3, nd.getSoluongghe());
            cs.setInt(4, nd.getMaloaitoatau()); 
            cs.setInt(5, nd.getTrangthai()); 

            int n = 0;
            try {
                n = cs.executeUpdate();
                if (n > 0) {
                    result = true;
                }
            } catch (Exception ex) {
                result = false;
            }
        }

        return result;
    }
    
    public static boolean CapNhatToaTau(ToaTau nd)throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException{
     boolean result = false;

        Connection conn = ConnectToMySql.getConnection();
        if (conn != null) {
            CallableStatement cs = conn.prepareCall("Update toatau Set    stttoa=?,matau=?,soluongghe=?,trangthai=?,maloaitoatau=?  Where matoatau = ?");
         
            // cs.setString(1, nd.get());
            cs.setInt(1, nd.getStttoa());
            cs.setInt(2, nd.getMatau());
            cs.setInt(3, nd.getSoluongghe());
            cs.setInt(4, nd.getTrangthai()); 
            cs.setInt(5, nd.getMaloaitoatau()); 
            cs.setInt(6, nd.getMatoatau()); 

            int n = 0;
            try {
                n = cs.executeUpdate();
                if (n > 0) {
                    result = true;
                }
            } catch (Exception ex) {
                result = false;
            }
        }

        return result;  
    }
    
    public static boolean XoaToaTau(String tenDangNhap)throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException{
       boolean result = false;

        Connection conn = ConnectToMySql.getConnection();
        if (conn != null) {
            CallableStatement cs = conn.prepareCall("delete from toatau where matoatau=?");
            cs.setString(1, tenDangNhap);

            int n = 0;
            try {
                n = cs.executeUpdate();
                if (n > 0) {
                    result = true;
                }
            } catch (Exception ex) {
                result = false;
            }
        }

        return result;
    }
    
     
}
